<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category PermissionController.php
 * @Depiction
 * @Author hwy <429208948@qq.com>
 * @Date 2022/4/7  17:56
 * +--------------------------------------------------------+
 */

namespace App\Http\Controllers\Admin\V1;

use App\Exceptions\ValidationException;
use App\Services\PermissionService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class PermissionController extends Controller
{
    protected $permissionService;

    public function __construct(PermissionService $permissionService)
    {
        $this->permissionService = $permissionService;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/8  17:47
     */
    public function index(Request $request): JsonResponse
    {
        $rules      = [
            'name'       => ['nullable', 'string', 'between:2,60',],
            'title'      => ['nullable', 'string', 'between:2,60',],
            'path'       => ['nullable', 'string', 'between:2,60',],
            'guard_name' => ['required', 'string', Rule::in(['api', 'admin']),],
        ];
        $attributes = [
            'name'       => '权限标识',
            'title'      => '目录名称',
            'path'       => '目录路径',
            'guard_name' => '目录对应规则名称',
        ];
        $messages   = [];
        $validated  = $this->validate($request, $rules, $messages, $attributes);

        $data = $this->permissionService->getList($validated);

        return $this->responseSuccessData('请求成功！', $data);

    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/8  17:55
     */
    public function show(Request $request)
    {
        $id = $request->get('id', 0);

        $data = $this->permissionService->getInfo($id);

        return $this->responseSuccessData('请求成功！', $data);

    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @Depiction 获取权限列表 Tree 结构
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/8  17:47
     */
    public function indexTree(Request $request): JsonResponse
    {
        $rules      = [
            'guard_name' => ['required', 'string', Rule::in(['api', 'admin']),],
        ];
        $attributes = [
            'guard_name' => '目录对应规则名称',
        ];
        $messages   = [];
        $validated  = $this->validate($request, $rules, $messages, $attributes);

        $data = $this->permissionService->getTree($validated);

        return $this->responseSuccessData('请求成功！', $data);
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @Depiction 更改排序与层级
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/11  13:34
     */
    public function drop(Request $request)
    {
        $tableNames = config('permission.table_names');
        $rules      = [
            'dragging' => ['required', 'integer', 'exists:' . $tableNames['permissions'] . ',id',],
            'drop'     => ['required', 'integer', 'different:dragging', 'exists:' . $tableNames['permissions'] . ',id',],
            'type'     => ['required', 'string', Rule::in(['before', 'after', 'inner']),],
        ];
        $attributes = [
            'dragging'   => '目录/权限',
            'drop'       => '目录/权限',
            'guard_name' => '目录对应规则名称',
        ];
        $messages   = [];
        $validated  = $this->validate($request, $rules, $messages, $attributes);

        $result = $this->permissionService->drop($validated);

        if ($result) {
            return $this->responseSuccessData('修改成功！');
        } else {
            return $this->responseErrorData('修改失败！');
        }

    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/11  13:34
     */
    public function create(Request $request)
    {
        $tableNames = config('permission.table_names');
        $rules      = [
            'pid'        => ['nullable', 'integer', 'exists:' . $tableNames['permissions'] . ',id',],
            'name'       => ['required', 'string', 'between:2,60', 'unique:' . $tableNames['permissions'],],
            'title'      => ['required', 'string', 'between:2,60',],
            'icon'       => ['required', 'string', 'between:2,60',],
            'path'       => ['required', 'string', 'between:2,60', 'unique:' . $tableNames['permissions'],],
            'component'  => ['required', 'string', 'between:2,60',],
            'guard_name' => ['required', 'string', 'between:2,60', Rule::in(['api', 'admin']),],
            'sort'       => ['nullable', 'numeric', 'between:1,999',],
            'hidden'     => ['nullable', 'numeric', 'between:0,1',],
        ];
        $attributes = [
            'pid'        => '上级目录',
            'name'       => '权限标识',
            'title'      => '目录名称',
            'icon'       => '目录图标',
            'path'       => '目录路径',
            'component'  => '目录地址',
            'guard_name' => '对应规则',
            'sort'       => '目录排序',
            'hidden'     => '目录显示',
        ];
        $messages   = [];
        $validated  = $this->validate($request, $rules, $messages, $attributes);

        $this->permissionService->create($validated);

        return $this->responseSuccessData('创建成功！');
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/11  13:34
     */
    public function update(Request $request)
    {
        $permission = $this->permissionService->getInfo($request->post('id', 0));
        if (empty($permission)) {
            return $this->responseErrorData('权限不存在！');
        }

        $tableNames = config('permission.table_names');
        $rules      = [
            'id'         => ['required', 'integer',],
            'pid'        => ['nullable', 'integer', 'different:id', 'exists:' . $tableNames['permissions'] . ',id',],
            'name'       => ['required', 'string', 'between:2,60', Rule::unique($tableNames['permissions'])->ignore($permission),],
            'title'      => ['required', 'string', 'between:2,60',],
            'icon'       => ['required', 'string', 'between:2,60',],
            'path'       => ['required', 'string', 'between:2,60', Rule::unique($tableNames['permissions'])->ignore($permission),],
            'component'  => ['required', 'string', 'between:2,60',],
            'guard_name' => ['required', 'string', 'between:2,60', Rule::in(['api', 'admin']),],
            'sort'       => ['nullable', 'numeric', 'between:1,999',],
            'hidden'     => ['nullable', 'numeric', 'between:0,1',],
        ];
        $attributes = [
            'id'         => 'ID',
            'pid'        => '上级目录',
            'name'       => '权限标识',
            'title'      => '目录名称',
            'icon'       => '目录图标',
            'path'       => '目录路径',
            'component'  => '目录地址',
            'guard_name' => '对应规则',
            'sort'       => '目录排序',
            'hidden'     => '目录显示',
        ];
        $messages   = [];
        $validated  = $this->validate($request, $rules, $messages, $attributes);

        $result = $this->permissionService->update($permission, $validated);

        if ($result) {
            return $this->responseSuccessData('修改成功！');
        } else {
            return $this->responseErrorData('修改失败！');
        }

    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/11  13:34
     */
    public function delete(Request $request)
    {
        $id         = $request->post('id', 0);
        $permission = $this->permissionService->getInfo($id);
        if (empty($permission)) {
            return $this->responseErrorData('用户不存在！');
        }

        $result = $this->permissionService->delete($permission);

        if ($result) {
            return $this->responseSuccessData('删除成功！');
        } else {
            return $this->responseErrorData('删除失败！');
        }
    }


}
